<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 图像区域 -->
      <div class="avatar-box">
        <img src="../../assets/logo.png"
             alt="">
      </div>
      <!-- 表单区 -->
      <el-form ref="loginFromRef"
               :rules="loginFromRules"
               label-width="0"
               class="login-form"
               :model="loginFrom">
        <el-form-item prop="code">
          <el-input placeholder="请输入账号"
                    prefix-icon="el-icon-user"
                    v-model="loginFrom.code"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码"
                    prefix-icon="el-icon-lock"
                    v-model="loginFrom.password"
                    type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary"
                     @click="login">登录</el-button>
          <el-button type="info"
                     @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 登录页面数据绑定对象
      loginFrom: {
        code: '',
        password: ''
      },
      // 登录页验证规则对象
      loginFromRules: {
        // 账号验证
        code: [
          { required: true, message: '请输入账号名称', trigger: 'blur' },
          { min: 4, max: 6, message: '长度在 4 到 6 个字符', trigger: 'blur' }
        ],
        // 密码验证
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetLoginForm () {
      this.$refs.loginFromRef.resetFields()
    },
    // 登录方法
    login () {
      this.$refs.loginFromRef.validate(async valid => { // async
        if (valid) {
          const { data: res } = await this.$http.post(this.$route.path, this.loginFrom)// await
          if (res.state) {
            sessionStorage.setItem('setToken', res.data.token)
            sessionStorage.setItem('setUser', JSON.stringify(res.data.user))
            // 页面跳转
            this.$router.replace('/content')
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error',
              duration: 5000
            })
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar-box {
    height: 100px;
    width: 100px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 80px;
  box-sizing: border-box;
}
</style>
